#include <iostream>

#include "debug.hpp"
#include "application.hpp"

namespace lge2d
{

Application::Application()
{
}

void Application::Initialize()
{
	int screen_width = m_script.GetGlobal("WINDOW_WIDTH", 800);
	int screen_height = m_script.GetGlobal("WINDOW_HEIGHT", 600);
	int screen_bpp = m_script.GetGlobal("WINDOW_BPP", 32);
	std::string screen_title = m_script.GetGlobal("WINDOW_TITLE",
			"game console");
	sf::VideoMode VideoMode(screen_width, screen_height, screen_bpp);

	unsigned long WindowStyle = m_script.GetGlobal("WINDOW_STYLE",
			sf::Style::Resize | sf::Style::Close);

	unsigned long depth = m_script.GetGlobal("WINDOW_DEPTH", 24);
	unsigned long stensil = m_script.GetGlobal("WINDOW_STENSIL", 8);
	unsigned long antialising = m_script.GetGlobal("WINDOW_ANTIALISING", 0);
	bool use_vsync = m_script.GetGlobal("WINDOW_VSYNC", 1);

	std::string icon_file = m_script.GetGlobal("WINDOW_ICON", "icon.png");

	sf::WindowSettings Params(depth, stensil, antialising);
	m_window.Create(VideoMode, screen_title, WindowStyle, Params);

	m_window.UseVerticalSync(use_vsync);

	if (!m_icon.LoadFromFile(icon_file))
	{
		std::clog << "can't load icon file: " << icon_file << std::endl;
	}
	else
	{
		m_window.SetIcon(m_icon.GetWidth(), m_icon.GetHeight(),
				m_icon.GetPixelsPtr());
	}
	std::clog << "create window\n";

	std::string font_file = m_script.GetGlobal("SYSTEM_FONT",
			"UbuntuMono-R.ttf");
	unsigned int font_size = m_script.GetGlobal("SYSTEM_FONT_SIZE", 14);

	if (!m_system_default_font.LoadFromFile(font_file, font_size))
	{
		fire_exception("can't load default font file: %s", font_file.c_str());
	}

	m_console.SetFont(&m_system_default_font);
	m_console.SetScript(&m_script);
	m_console.SetWindow(&m_window);

	m_console.Initialize();

	m_info_bar.SetFont(&m_system_default_font);
	m_info_bar.SetWindow(&m_window);

	m_info_bar.Initialize();
	//TODO: remove test start
	m_game_server.Start();

	glPointSize(2.f);

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	double aspect_ratio = ((double) screen_width / screen_height);
	glOrtho(-1.0 * aspect_ratio, 1.0 * aspect_ratio, -1.0, 1.0, -1.0, 1.0);
	glScalef(0.5f, 0.5f, 0);
	glMatrixMode(GL_MODELVIEW);

	glViewport(0, 0, screen_width, screen_height);

	m_game.SetWindow(&m_window);
	m_game.Initialize();
}

void Application::StartGameLoop()
{
	bool loop = true;
	while (loop)
	{
		sf::Event event;
		while (m_window.GetEvent(event))
		{
			if (m_console.OnEvent(event))
			{
				break;
			}
			else if (m_info_bar.OnEvent(event))
			{
				break;
			}
			else if ((event.Type == sf::Event::KeyPressed
					&& event.Key.Code == sf::Key::Escape) ||
					(event.Type == sf::Event::Closed))
			{
				loop = false;
				break;
			}
			else
			{
				m_game.OnEvent(event);
			}
		}

		const float dt = m_frame_clock.GetElapsedTime();
		m_frame_clock.Reset();

		m_game_server.Update(dt);

		m_game.Update(dt);
		m_console.Update(dt);
		m_info_bar.Update(dt);

		m_window.Clear();

		m_game.Render();
		m_console.Render();
		m_info_bar.Render();

		m_window.Display();
		sf::Sleep(0.001f);
	}
}

Application::~Application()
{

}

} /* namespace lge2d */
